// 封装localstorage
import Vue from '../main'
const common = {
  timestampToTime:function (timestamp){
    // var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    if (timestamp){
      let date = new Date(timestamp)//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      // alert(date)
      let Y = date.getFullYear() + '-'
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()

      let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'
      let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':'
      let s = date.getSeconds() < 10 ? '0' + date.getSeconds() + '' : date.getSeconds()
      return Y + M + D + ' ' + h + m + s
    } else {
      return '- -'
    }
  },
  timestampSimple:function (timestamp){
    // var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    if (timestamp){
      let date = new Date(timestamp)//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      // alert(date)
      let Y = date.getFullYear() + '-'
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ''
      return Y + M + D
    } else {
      return ''
    }
  },
  refrain:function (arr){
    let tmp = []
    if (Array.isArray(arr)){
      arr.concat().sort().sort(function (a, b){
        if (a == b && tmp.indexOf(a) === -1) tmp.push(a)
      })
    }
    return tmp
  },

  // 获取日期范围内的所有日期列表
  getDateRange(begin, end){
    let arr1 = begin.split('-')   // 这里可以换成/  2021/01/01
    let arr2 = end.split('-')
    let arr1_ = new Date()
    let arrTime = []
    arr1_.setUTCFullYear(arr1[0], arr1[1] - 1, arr1[2])
    let arr2_ = new Date()
    arr2_.setUTCFullYear(arr2[0], arr2[1] - 1, arr2[2])
    let unixDb = arr1_.getTime()
    let unixDe = arr2_.getTime()
    for (let k = unixDb; k <= unixDe;){
      arrTime.push(this.dateTimeParse(k, 'YY-MM-DD'))
      k = k + 24 * 60 * 60 * 1000
    }
    return arrTime
  },
  // 时间格式处理
  dateTimeParse(timestamp, format, prefix){
    if (typeof timestamp === 'string'){
      timestamp = Number(timestamp)
    }
    //转换时区
    let currentZoneTime = new Date(timestamp)
    let currentTimestamp = currentZoneTime.getTime()
    let offsetZone = currentZoneTime.getTimezoneOffset() / 60 //如果offsetZone>0是西区，西区晚
    let offset = null
    //客户端时间与服务器时间保持一致，固定北京时间东八区。
    offset = offsetZone + 8
    currentTimestamp = currentTimestamp + offset * 3600 * 1000
    let newTimeStamp = null
    if (currentTimestamp){
      if (currentTimestamp.toString().length === 13){
        newTimeStamp = currentTimestamp.toString()
      } else if (currentTimestamp.toString().length === 10){
        newTimeStamp = currentTimestamp + '000'
      } else {
        newTimeStamp = null
      }
    } else {
      newTimeStamp = null
    }
    let dateobj = newTimeStamp ? new Date(parseInt(newTimeStamp)) : new Date()
    let YYYY = dateobj.getFullYear()
    let MM = dateobj.getMonth() > 8 ? dateobj.getMonth() + 1 : '0' + (dateobj.getMonth() + 1)
    let DD = dateobj.getDate() > 9 ? dateobj.getDate() : '0' + dateobj.getDate()
    let HH = dateobj.getHours() > 9 ? dateobj.getHours() : '0' + dateobj.getHours()
    let mm = dateobj.getMinutes() > 9 ? dateobj.getMinutes() : '0' + dateobj.getMinutes()
    let ss = dateobj.getSeconds() > 9 ? dateobj.getSeconds() : '0' + dateobj.getSeconds()
    let output = ''
    let separator = '/'
    if (format){
      separator = format.match(/-/) ? '-' : '/'
      output += format.match(/yy/i) ? YYYY : ''
      output += format.match(/MM/) ? (output.length ? separator : '') + MM : ''
      output += format.match(/dd/i) ? (output.length ? separator : '') + DD : ''
      output += format.match(/hh/i) ? (output.length ? ' ' : '') + HH : ''
      output += format.match(/mm/) ? (output.length ? ':' : '') + mm : ''
      output += format.match(/ss/i) ? (output.length ? ':' : '') + ss : ''
    } else {
      output += YYYY + separator + MM + separator + DD
    }
    output = prefix ? (prefix + output) : output
    return newTimeStamp ? output : ''
  },

  // 接口返回文件流数据，生成导出excel文件
  exportMethod(url){
    Vue.$axios({
      method:'get',
      url,
      responseType:'blob'
    }).then(({data,headers}) => {
      let blob = new Blob([data], {type:'application/vnd.ms-excel,charset=utf-8'})
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = window.URL.createObjectURL(blob)
      let fileName = decodeURI(headers['content-disposition'].split(';')[1].split('=')[1]) || '分拣表.xlsx'
      link.download = fileName
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    })
  }

}

export default common